package common.hua.com.http;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.TextView;
import com.hua.common.commontools.NotificationUtil;
import com.hua.common.http.HttpUtil;

import java.io.File;
import common.hua.com.myapplication.R;

/**
 * Created by hxl on 2016/3/21.
 */
public class HttptestAcitity extends Activity implements View.OnClickListener {
    private TextView textView = null;
    private Button btn = null;
    private Button btn2=null;
    private Notification notification=null;
    private NotificationManager notificationManager=null;
    private int myprogress=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.httptest_layout);
        textView = (TextView) this.findViewById(R.id.tv);
        this.btn = (Button) this.findViewById(R.id.bt);
        this.btn.setOnClickListener(this);
        btn2= (Button) this.findViewById(R.id.bt2);
        this.btn2.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt:
                downFile();
                sendNofi();
                break;
            case R.id.bt2:
                break;
        }
    }

    private void sendNofi(){
        this.notification= NotificationUtil.createCustomNotif(this,new Intent(),new RemoteViews(getPackageName(),R.layout.notificationlayout));
        notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(10,this.notification);
    }



    private void get() {
        HttpUtil.getInstance().getReques("http://photo.enterdesk.com/2011-2-16/enterdesk.com-1AA0C93EFFA51E6D7EFE1AE7B671951F.jpg", new HttpUtil.HttpUtilCallBack() {
            @Override
            public void onFailure(int code) {
                textView.setText(code + "");
            }

            @Override
            public void onResponse(String responseBody) {
                textView.setText(responseBody);
            }
        });
    }

    private void downFile() {
//        String url = "http://192.168.6.26:8080/test01/file/001.mp4";
        String url = "http://p.gdown.baidu.com/c77b37a55728aae148178afdf612826c98274d801509e67e7644621dfb2146c5550f8dcfeb97b2628dd3581778705c7fda2ef4d73c383867b99bb793274b1e1938b02079ec273bed37ffdca27f73f04f8b7d28dd72c43e954852f225c16e119098cfd0223b2538d8b6d5581b78923c66713775e476464fcb1538926fee90cd00d5ca7e49521f2d7c6bcdae8034dff1129ea0e90bda7854ce1b6468951af9a2ddeb89364857caa143866a00db833176e3a423a3cdb07ae98e5fa885367dc360b26254411a6e70738a";
//        String url="http://photo.enterdesk.com/2011-2-16/enterdesk.com-1AA0C93EFFA51E6D7EFE1AE7B671951F.jpg";
//        String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + File.separator + "temp" + File.separator ;
        String filePath = Environment.getExternalStorageDirectory() + File.separator  + "temp" + File.separator ;
        HttpUtil.getInstance().downloadFile(url, filePath,"huaxiaolin.apk", new HttpUtil.HttpDownloadprogressListener() {
            @Override
            public void progress(int progress, float fileCount, boolean isFinish) {
                textView.setText("已下载：" + progress + "    总量：" + fileCount / (1024 * 1024) + "M");
                if(notification!=null){
                    if(myprogress<progress) {
                        RemoteViews remoteViews = notification.contentView;
                        remoteViews.setProgressBar(R.id.content_view_progress, 100, progress, false);
//                        notification.flags = Notification.FLAG_ONGOING_EVENT;
                        notificationManager.notify(10, notification);
                        myprogress=progress;
                    }
                }
            }
        });
    }
}
